<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/js/jquery-3.2.1.min.js"></script>
<script>
		$(document).ready(function(){
			//默认隐藏更新学生板块
			$("div#updateStudent").hide();
			//查询学生请求
			function findStudentById(id) {
				$.ajax({
					url: "/Class/findClassById/"+id,
					type: "get",
					dataType: "json",
					success: function(data) {
						
						
						//判断0或1 数据库存0/1
						console.log(data);
						
						//每次进来先清空
						$("#tr2").remove();
						
						$("<tr id='tr2'><td>"+data.id+"</td>"+
						  "<td>"+data.classname+"</td>"+
						  '<td><a href="/Class/deleteClass/'+data.id+'" class="delete" >删除</a></td>'+
						  '<td><a href="#" class="update" id='+data.id+'>更新</a></td>').appendTo($("#findStuTable"));
						
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						alert("查找失败，找不到该班级！");
						/* alert(XMLHttpRequest.status);
						alert(XMLHttpRequest.readyState);
						alert(textStatus); */
					},
				})
			}
			//点击查询学生
			$("#submit").click(function(){
				var id = $("#stuId").val();
				findStudentById(id);
			});
			//更新学生页面
			$("div#findStudent").on("click","a.update",function(){
				var id = $(this).attr("id");
				//alert(id);
				$("div#findStudent").hide();
				$("div#updateStudent").show();
				$.ajax({
					url: "/Class/findClassById/"+id,
					type: "get",
					dataType: "json",
					success: function(data) {
						
						$("#sid").val(data.id);
						$("#sname").val(data.classname);
						
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						alert("查找失败，找不到该班级！");
						
					},
				})
			});
			//更新学生
			function updateCustomer() {
			$.post("/Class/updateClassById",$("#updateForm").serialize(),function(data){
				alert("信息更新成功！");
				window.location.reload();
				});
			}
			$("#updateStudentSubmit").click(function(){
				updateCustomer();
			});
			//end
		});
	</script>
</head>
	<body>

	<center>
	
	<div id="findStudent">
	<p>编号：<input type="text" name="stuId" id="stuId"/><input type="button" id="submit" value="查询" /></p>
		<table border="1" id="findStuTable">
		
			<tr id="tr1">
				<td>编号</td>
				<td>班级</td>
				<td>删除</td>
				<td>更新</td>
			</tr>

		</table>
	</div>
	<!-- update -->
	<div id="updateStudent">
	<form id="updateForm">
		编号:<input type="text" name="id" id="sid" readonly="readonly"><br>
		班级:<input type="text" name="classname" id="sname"><br>
		<input type="button" id="updateStudentSubmit" value="更新"/>
	</form>		
		
	</div>
	</center>
	</body>
</html>
